package com.jt.shop.controller;

import com.jt.shop.model.User;
import com.jt.shop.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by he on 2015/9/5.
 */
@RequestMapping("/user")
@Controller
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/add")
    public String add() {
        return "user/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(User user, BindingResult br, Model model) {
        if (br.hasErrors()) {
            return "user/add";
        }
        int count = userService.add(user);
        if (count > 0) {
            model.addAttribute("result", "添加成功");
        } else {
            model.addAttribute("result", "添加失败");
        }
        return "user/add";
    }

    @RequestMapping("/home")
    public String home(HttpSession session) {
        return "user/home";
    }
}
